package org.opensource.yabts.client;

import org.opensource.yabts.server.bean.Project;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.FormItemErrorFormatter;
import com.smartgwt.client.widgets.form.fields.CheckboxItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class LoginForm extends VLayout  {
    private DynamicForm form;
    private TextItem usernameItem;
    private PasswordItem pwdItem;
    private CheckboxItem keepCBox;
    private IButton button;
	private Label errLabel = new Label();
	private Window modalWindow;

	private final AuthServiceAsync authService = GWT.create(AuthService.class);
	private final GreetingServiceAsync greetService = GWT.create(GreetingService.class);
	private final Messages messages = GWT.create(Messages.class);
	
    public LoginForm() {
    	createForm();
    }
    
    public LoginForm(Window modalWindow) {
    	this.modalWindow = modalWindow;
    	createForm();
    }

    public void createForm() {
    	setPadding(10);
    	
        form = new DynamicForm();
        form.setLayoutAlign(Alignment.CENTER);
        form.setWidth100();
        form.setErrorOrientation(FormErrorOrientation.TOP);
//        form.showFieldErrors("username");
//        form.setShowInlineErrors(true);
        form.setShowErrorIcons(false);
        form.setShowErrorText(true);
       
//        form.setErrorItemCellStyle("errorStyle");

        usernameItem = new TextItem("username", messages.usernameField());
        usernameItem.setRequired(true);
        usernameItem.setWidth(200);
//        usernameItem.setCellStyle("gwt-CellStyle");
//        usernameItem.setTitleStyle("gwt-formTitle");
//        usernameItem.setEmptyDisplayValue("name@company.com");

        pwdItem = new PasswordItem("password", messages.passwordField());
        pwdItem.setRequired(true);

        keepCBox = new CheckboxItem();
        keepCBox.setTitle("Remind me");
        keepCBox.setRequired(true);
        
        errLabel.setHeight(30);
//        errLabel.setWidth100();
        errLabel.setLayoutAlign(Alignment.LEFT);
        errLabel.setStyleName("gwt-Label-error");

        form.setFields(usernameItem, pwdItem, keepCBox);

        button = new IButton();
        button.setTitle("Submit");
        button.setLayoutAlign(Alignment.CENTER);
        button.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
//				final Window winModal = new Window();
//				
//		        winModal.setWidth(360);
//		        winModal.setHeight(115);
//		        winModal.setTitle("Modal Window");
//		        winModal.setShowMinimizeButton(false);
//		        winModal.setIsModal(true);
//		        winModal.setShowModalMask(true);
//		        winModal.centerInPage();
//	            DynamicForm form = new DynamicForm();
//	            form.setHeight100();
//	            form.setWidth100();
//	            form.setPadding(5);
//	            form.setLayoutAlign(VerticalAlignment.BOTTOM);
//	            TextItem textItem = new TextItem();
//	            textItem.setTitle("Text");
//	            DateItem dateItem = new DateItem();
//	            dateItem.setTitle("Date");
//	            DateItem dateItem2 = new DateItem();
//	            dateItem2.setTitle("Date");
//	            dateItem2.setUseTextField(true);
//	            form.setFields(textItem, dateItem, dateItem2);
//	            winModal.addItem(form);
//		        winModal.show();
				authService.doLogin(usernameItem.getEnteredValue(), pwdItem.getEnteredValue(), new AsyncCallback<Void>() {

					public void onSuccess(Void result) {
						errLabel.setContents("Hello user!");
					}

					public void onFailure(Throwable caught) {
						errLabel.setContents(caught.getMessage());						
					}

					
				});
			}
		});

        addMember(errLabel);
        addMember(form);
        addMember(button);
        
        IButton but = new IButton("greet");
        but.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
//				greetService.greetServer("op", new GeneralAsyncCallback<String>() {
//
//					public void onSuccess(String result) {
//						// TODO Auto-generated method stub
//						
//					}
//
//					@Override
//					protected void catchFailure(Throwable caught) {
//						// TODO Auto-generated method stub
//						
//					}
//					
//				});
				greetService.getProject(Long.valueOf(1), new GeneralAsyncCallback<Project>() {

					public void onSuccess(Project result) {
						// TODO Auto-generated method stub
						
					}

					@Override
					protected void catchFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}
				});
				
			}
		});
        addMember(but);
    }
}
